package com.tarena.lbs.basic.web.repository.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.mapper.UserGroupMapper;
import com.tarena.lbs.basic.web.repository.UserGroupRepository;
import com.tarena.lbs.common.basic.converter.UserGroupConverter;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.query.MarketingSelectedQuery;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {

    @Resource
    private UserGroupMapper userGroupMapper;

    @Resource
    private UserGroupConverter userGroupConverter;

    @Override
    public List<UserGroupDO> selectByIds(List<Integer> ids) {
        return userGroupMapper.selectByIds(ids);
    }

    @Override public BasePageList<UserGroupBO> list(UserGroupQuery query) {
        int page = query.getPageNo();
        int pageSize = query.getPageSize();
        //new PageQuery(page, pageSize);
        List<UserGroupDO> list = userGroupMapper.selectList(query);
        PageInfo<UserGroupDO> pageInfo = new PageInfo<>(list);
        List<UserGroupBO> boList = pageInfo.getList().stream().map(storeDO -> {
            UserGroupBO bo = new UserGroupBO();
            BeanUtils.copyProperties(storeDO, bo);
            return bo;
        }).collect(Collectors.toList());
        return new BasePageList<>(boList,Long.valueOf(pageInfo.getTotal()));
    }

    @Override public BasePageList<UserGroupBO> marketingUserGroupList(MarketingSelectedQuery query) {
        int page = query.getPageNo();
        int pageSize = query.getPageSize();
        //new PageQuery(page, pageSize);
        List<Integer> userGroupIdList = new ArrayList<>();
        for (Long temp : query.getIdList()) {
            userGroupIdList.add(temp.intValue());
        }
        List<UserGroupDO> list = userGroupMapper.selectByIds(userGroupIdList);
        PageInfo<UserGroupDO> pageInfo = new PageInfo<>(list);
        List<UserGroupBO> boList = pageInfo.getList().stream().map(storeDO -> {
            UserGroupBO bo = new UserGroupBO();
            BeanUtils.copyProperties(storeDO, bo);
            return bo;
        }).collect(Collectors.toList());
        return new BasePageList<>(boList,Long.valueOf(pageInfo.getSize()));
    }

    @Override public UserGroupBO getDetail(Integer id) {
        UserGroupDO userDO = userGroupMapper.selectById(id);
        return userGroupConverter.do2bo(userDO);
    }

    @Override
    public void insert(UserGroupParam param) {
        UserGroupDO userGroupDO = userGroupConverter.param2DO(param);
        userGroupMapper.insert(userGroupDO);
    }
}
